﻿using System;
using System.Windows.Forms;
using Daq331m;

namespace Counter_Demo
{
    public partial class Form1 : Form
    {
        private readonly Daq331M _daq331;
        private DataGridViewRow _pulseRow;
        private DataGridViewRow _countRow;
        private DataGridViewRow _frequencyRow;
        public Form1()
        {
            InitializeComponent();
            
            _daq331 = new Daq331M();

            _countRow = new DataGridViewRow();
            _countRow.CreateCells(this.dataGridView1);
            _countRow.Cells[0].Value = "计数";
            dataGridView1.Rows.Insert(0, _countRow);

            _frequencyRow = new DataGridViewRow();
            _frequencyRow.CreateCells(this.dataGridView1);
            _frequencyRow.Cells[0].Value = "频率(Hz)";
            dataGridView1.Rows.Insert(1, _frequencyRow);

            _pulseRow = new DataGridViewRow();
            _pulseRow.CreateCells(this.dataGridView1);
            _pulseRow.Cells[0].Value = "脉宽(us)";
            dataGridView1.Rows.Insert(2, _pulseRow);

            comboBox1.SelectedIndex = 0;
            comboBox2.SelectedIndex = 0;
            comboBox3.SelectedIndex = 0;
        }

        private void connectButton_Click(object sender, EventArgs e)
        {
            _daq331.DeleteDevice();
            _daq331.CreateDevice();

            _daq331.UseTcpClientBackend("192.168.1.100", 8000);

            // 初始化设备
            Console.WriteLine(!_daq331.InitializeDevice() ? @"DAQ331M设备初始化失败" : @"DAQ331M设备初始化成功");
        }

        private void configButton_Click(object sender, EventArgs e)
        {
            var channel = (uint)comboBox1.SelectedIndex + 1;
            var mode = (uint)comboBox2.SelectedIndex + 1;
            var type = (uint)comboBox3.SelectedIndex + 1;
            
            Console.WriteLine(!_daq331.ConfigCounter(channel, mode, type) ? @"DAQ331M计数器配置失败" : @"DAQ331M计数器配置成功");
        }

        private void startButton_Click(object sender, EventArgs e)
        {
            var channel = (uint)comboBox1.SelectedIndex + 1;
            
            Console.WriteLine(!_daq331.StartCounter(channel) ? @"DAQ331M计数器开启失败" : @"DAQ331M计数器开启成功");
        }

        private void stopButton_Click(object sender, EventArgs e)
        {
            var channel = (uint)comboBox1.SelectedIndex + 1;
            
            Console.WriteLine(!_daq331.StopCounter(channel) ? @"DAQ331M计数器关闭失败" : @"DAQ331M计数器关闭成功");
        }

        private void readButton_Click(object sender, EventArgs e)
        {
            var data = _daq331.ReadCounter();

            for (var i = 0; i < 2; i++)
            {
                var dataStr = data[i].ToString();
                _countRow.Cells[i + 1].Value = dataStr;
            }

            for (var i = 0; i < 2; i++)
            {
                var index = 2 + i;
                var dataStr = data[index].ToString();
                _frequencyRow.Cells[i + 1].Value = dataStr;
            }
            
            for (var i = 0; i < 2; i++)
            {
                var index = 4 + i;
                var dataStr = data[index].ToString();
                _pulseRow.Cells[i + 1].Value = dataStr;
            }
            
        }

        private void clearButton_Click(object sender, EventArgs e)
        {
            var channel = (uint)comboBox1.SelectedIndex + 1;
            
            Console.WriteLine(!_daq331.ClearCounter(channel) ? @"DAQ331M计数器清除失败" : @"DAQ331M计数器清除成功");
        }
    }
}
